package com.bphx.android.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.bphx.android.network.Downloader;
import com.bphx.android.network.ManagerStatus;
import com.bphx.android.ui.listeners.IChangeStatusListener;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.bphx.android.R.id.*;
import static com.bphx.android.R.layout.download_dialog;
import static com.bphx.android.R.string.*;
import static com.bphx.android.network.ManagerStatus.valueOf;
import static com.bphx.android.utils.ChangeStatusProxy.getInstance;
import static com.bphx.android.utils.Constants.*;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class DownloadStatusActivity extends Activity implements IChangeStatusListener<ManagerStatus> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(textPleaseWait);
        setContentView(download_dialog);
        getInstance().putListener(DownloadStatusActivity.class.getName(), this);

        this.userName = getIntent().getExtras().getString(USER_NAME);
        this.password = getIntent().getExtras().getString(PASSWORD);
        this.statusString = (TextView) findViewById(caption);

        okButtonView = (Button) findViewById(okButton);
        okButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownloadStatusActivity.this.finish();
            }
        });

        if (savedInstanceState == null) {

            currentStatusParams = new String[0];
        } else {

            String stateString = savedInstanceState.getString(CURRENT_STATE);
            String[] stateParams = savedInstanceState.getStringArray(CURRENT_STATE_PARAMS);
            changeStatus(valueOf(stateString), stateParams);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString(CURRENT_STATE, currentStatus.name());
        outState.putStringArray(CURRENT_STATE_PARAMS, currentStatusParams);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        getInstance().removeListener(DownloadStatusActivity.class.getName());
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Downloader downloader = new Downloader(this);
        downloader.execute(userName, password);
    }

    @Override
    public void changeStatus(ManagerStatus status, String... params) {

        currentStatus = status;
        currentStatusParams = params;

        switch (status) {
            case DEFAULT:
                setAsProcessed();
                break;
            case DOWNLOAD_STARTED:
                setStatusString(statusDownloadStarted);
                setAsProcessed();
                break;
            case DOWNLOAD_FINISHED:
                setStatusString(statusDownloadFinished);
                setAsProcessed();
                break;
            case MERGE_STARTED:
                setStatusString(statusMergingStarted);
                setAsProcessed();
                break;
            case MERGE_FINISHED:
                setStatusString(statusMergingFinished);
                setAsProcessed();
                break;
            case ERROR:
                setStatusString(errorWithMessage, params);
                setTitle(error);
                setAsCloseable();
                break;
            case SUCCESS:
                setStatusString(statusUpdateSuccess);
                setTitle(success);
                setAsCloseable();
                break;
        }
    }

    private void setAsCloseable() {
        findViewById(progressbar).setVisibility(INVISIBLE);
        okButtonView.setVisibility(VISIBLE);
    }

    private void setAsProcessed() {
        findViewById(progressbar).setVisibility(VISIBLE);
        okButtonView.setVisibility(INVISIBLE);
    }

    private void setStatusString(int id, String... params) {
        if (params == null || params.length == 0) {
            statusString.setText(id);
        } else {
            statusString.setText(getString(id, params));
        }
    }

    private TextView statusString;
    private Button okButtonView;

    private String userName;
    private String password;

    //use for saving state only
    private ManagerStatus currentStatus;
    private String[] currentStatusParams;
}